//
//  RulesRowTemplate.m
//  Pennyworth
//
//  Created by Chris Karr on 7/28/08.
//  Copyright 2008 Northwestern University. All rights reserved.
//

#import "RulesRowTemplate.h"

@implementation RulesRowTemplate

- (double) matchForPredicate:(NSPredicate *) predicate
{
	if ([predicate isKindOfClass:[NSComparisonPredicate class]])
		return 1.0;
	
	return [super matchForPredicate:predicate];
}

- (NSArray *) displayableSubpredicatesOfPredicate:(NSPredicate *) predicate
{
	return nil;
}

- (void) setPredicate:(NSPredicate *) predicate
{
	NSArray * views = [self templateViews];
	
	if (predicate == nil)
	{
		[((NSPopUpButton *) [views objectAtIndex:0]) selectItemAtIndex:0];
		[((NSPopUpButton *) [views objectAtIndex:1]) selectItemAtIndex:0];
	}
	else
	{
		NSComparisonPredicate * comp = (NSComparisonPredicate *) predicate;
		
		NSString * left = [[comp leftExpression] keyPath];
		
		if ([((NSPopUpButton *) [views objectAtIndex:0]) numberOfItems] > 1)
			[((NSPopUpButton *) [views objectAtIndex:0]) selectItemWithTitle:left];
		else
			[((NSPopUpButton *) [views objectAtIndex:0]) selectItemAtIndex:0];
		
		NSPredicateOperatorType operator = [comp predicateOperatorType];
		
		NSArray * operators = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSEqualToPredicateOperatorType],
							   [NSNumber numberWithInteger:NSNotEqualToPredicateOperatorType],
							   [NSNumber numberWithInteger:NSContainsPredicateOperatorType],
							   [NSNumber numberWithInteger:NSLessThanOrEqualToPredicateOperatorType],
							   [NSNumber numberWithInteger:NSGreaterThanOrEqualToPredicateOperatorType],
							   [NSNumber numberWithInteger:NSMatchesPredicateOperatorType],
							   [NSNumber numberWithInteger:NSLikePredicateOperatorType], nil];
		
		NSInteger index = [operators indexOfObject:[NSNumber numberWithInteger:operator]];
		
		[((NSPopUpButton *) [views objectAtIndex:1]) selectItemAtIndex:index];
		
		NSString * right = @"";
		
		if (predicate != nil)
			right = [[comp rightExpression] description];
		
		[((NSTextField *) [views objectAtIndex:2]) setObjectValue:right];
	}
}

@end
